package com.factory.simplefactory.pizzastore.order;

import com.factory.simplefactory.pizzastore.pizza.CheesePizza;
import com.factory.simplefactory.pizzastore.pizza.GreekPizza;
import com.factory.simplefactory.pizzastore.pizza.PepperPizza;
import com.factory.simplefactory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/4/13 10:02
 */
public class OrderPizza {
    
    /*public OrderPizza(){
        Pizza pizza = null;
        String orderType;// 订购披萨的类型
        do {
            orderType = getType();
            if (orderType.equals("greek")) {
                pizza = new GreekPizza();
                pizza.setName("希腊披萨");
            } else if (orderType.equals("cheese")) {
                pizza = new CheesePizza();
                pizza.setName("奶酪披萨");
            } else if (orderType.equals("pepper")){
                pizza = new PepperPizza();
                pizza.setName("胡椒披萨");
            }else {
                break;
            }
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        }while (true);
    }*/
    
    // 定义一个简单工厂对象
    SimpleFactory simpleFactory;
    Pizza pizza = null;
    
    public OrderPizza(SimpleFactory simpleFactory){
        setFactory(simpleFactory);
    }
    public void setFactory(SimpleFactory simpleFactory) {
        String orderType = ""; // 用户输入
        
        this.simpleFactory = simpleFactory;
        
        do {
            orderType = getType();
            pizza = simpleFactory.createPizza(orderType);
            if (pizza != null){
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else{
                System.out.println("订购披萨失败");
                break;
            }
        }while (true);
    } 
    
    // 写一个方法，可以动态的获取客户希望订购的披萨种类
    private String getType(){
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza type:");
            String str = br.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
    
}
